<?php

/**
 * File preprocess.inc
 *
 * includes preprocess handlers
 */

/**
 * implements hook_preprocess_image_style().
 */
function crabii_preprocess_image_style(&$vars) {
  if(isset($vars['skip-responsive']) && $vars['skip-responsive']) {
    return;
  }

  $vars['attributes']['class'][] = 'img-responsive'; // http://getbootstrap.com/css/#overview-responsive-images
}

/**
 * implements hook_preprocess_page().
 *
 * Preprocess page.tpl.php to inject the $search_box variable back.
 */
function crabii_preprocess_page(&$vars){
  $keys = '';
  if(arg(0) == 'search' && arg(1) == 'node') {
    $keys = trim(arg(2));
    // Also try to pull search keywords out of the $_REQUEST variable to
    // support old GET format of searches for existing links.
    if (!$keys && !empty($_REQUEST['keys'])) {
      $keys = trim($_REQUEST['keys']);
    }
  }

  $form = drupal_get_form('search_form', NULL, $keys);
  $form['#attributes']['class'][] = 'navbar-form';
  $form['#attributes']['role'] = 'search';
  $vars['search_box'] = render($form);

  user_inc_preprocess_page($vars);

  // HACK: Use custom 403 and 404 pages
  $header = drupal_get_http_header('status');
  if ($header == '404 Not Found') {
    $vars['theme_hook_suggestions'][] = 'page__simple';
  }
  if ($header == '403 Forbidden') {
    $vars['theme_hook_suggestions'][] = 'page__simple';
  }

  // modal forms
  global $user;

  drupal_add_js(drupal_get_path('module', 'crabii_app') . '/js/crabii-modals.js');

  $nip_node_form = NULL;
  if($user->uid) {
    module_load_include('inc', 'node', 'node.pages');
    $node = (object) array(
      'uid' => $user->uid,
      'name' => (isset($user->name) ? $user->name : ''),
      'type' => NIP_TYPE,
      'language' => LANGUAGE_NONE,
    );

    $title = drupal_get_title();  // get title because nip_node_form will reset title
    $nip_node_form = drupal_get_form('nip_node_form', $node);
    drupal_set_title($title, PASS_THROUGH);     // set title, safe to set PASS_THROUGH to keep HTML in title
  }
  $vars['crabii_modals'] = theme('crabii_modals', array('nip_node_form' => $nip_node_form));

  // detect if we should use different page template here
  $crabii_theme_customization = &drupal_static('crabii_theme_customization');
  if(!empty($crabii_theme_customization)) {
    if(isset($crabii_theme_customization['page_template'])) {
      $vars['theme_hook_suggestions'][] = $crabii_theme_customization['page_template'];
    }
    if(isset($crabii_theme_customization['show_messages'])) {
      $vars['show_messages'] = $crabii_theme_customization['show_messages'];
    }
  }
}

/**
 * implements hook_preprocess_html().
 */
function crabii_preprocess_html(&$vars) {
  drupal_add_css('//fonts.googleapis.com/css?family=Roboto:100italic,100,300italic,300,400italic,400,500italic,500,700italic,700,900italic,900', array('type' => 'external'));
  drupal_add_css('//fonts.googleapis.com/css?family=Open+Sans:700', array('type' => 'external'));
  drupal_add_js(array(
    'user' => $GLOBALS['user']
  ), 'setting');
}

/**
 * Processes variables for node.tpl.php
 *
 * Most themes utilize their own copy of node.tpl.php. The default is located
 * inside "modules/node/node.tpl.php". Look in there for the full list of
 * variables.
 *
 * The $vars array contains the following arguments:
 * - $node
 * - $view_mode
 * - $page
 *
 * @see node.tpl.php
 */
function crabii_preprocess_node(&$vars) {
  $node = $vars['node'];

  $field_board = field_get_items('node', $node, 'field_board');
  if(!empty($field_board)) {
    $tid = $field_board[0]['tid'];
    $board_name = TaxonomyHelper::taxonomy_get_term_name($tid);
    $field_board = l($board_name, "taxonomy/term/$tid", array(
      'attributes' => array('title' => "Go to $board_name")
    ));
  }

  if($node->type == NIP_TYPE) {
    $vars['submitted'] = t('!username<br />!datetime',
      array(
        '!username' => $vars['name'],
        '!datetime' =>  CommonHelper::format_time_ago($node->created) . (empty($board_name) ? '' : " in $field_board"),
      )
    );
  }
}

/**
 * implements hook_preprocess_comment().
 *
 * @param $vars
 */
function crabii_preprocess_comment(&$vars) {
  $comment = $vars['elements']['#comment'];
  if($comment->node_type == 'comment_node_nip') {
    $vars['submitted'] = t('!datetime',
      array(
        '!datetime' => CommonHelper::format_time_ago($comment->created),
      )
    );
  }
}

/**
 * Implements hook_preprocess_button().
 *
 * unset .btn-success class
 * @see bootstrap_preprocess_button().
 */
function crabii_preprocess_button(&$vars) {
  foreach($vars['element']['#attributes']['class'] as $index => $class) {
    if(in_array($class, array('btn-success', 'btn-info', 'btn-warning', 'btn-danger'))) {
      unset($vars['element']['#attributes']['class'][$index]);
    }
  }
}

/**
 * Process variables for search-result.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $result
 * - $module
 *
 * @see search-result.tpl.php
 */
function crabii_preprocess_search_result(&$vars) {
  if($vars['module'] != 'node') {
    return;
  }

  $node = $vars['result']['node'];

  if($node->type != NIP_TYPE) {
    return;
  }
  drupal_add_css(path_to_theme() . '/css/search.css');

  // loading libraries
  LibraryHelper::load(array(
    array('name' => 'salvattore',
      'options' => array('scope' => 'footer'),
    ),
    array('name' => 'jquery-timeago'),
  ));

  $field_board = field_get_items('node', $node, 'field_board');
  if(!empty($field_board)) {
    $tid = $field_board[0]['tid'];
    $board_name = TaxonomyHelper::taxonomy_get_term_name($tid);
    $field_board = l($board_name, "taxonomy/term/$tid", array(
      'attributes' => array('title' => "Go to $board_name")
    ));
  }

  $vars['submitted'] = t('!datetime',
    array(
      '!datetime' =>  CommonHelper::format_time_ago($node->created) . (empty($board_name) ? '' : " in $field_board"),
    )
  );

  $vars['user_picture'] = theme('user_picture', array('account' => $vars['user']));
  $vars['username'] = theme('username', array('account' => $vars['user']));
  $vars['og_data'] =  array(
    '#markup' => theme('link_helper_og_data_teaser', array('og_data' => $node->link_crabber->og_data, 'node' => $node)),
    '#weight' => 20,
  );
}
